<?php
/*
 * СПВ
 **/
class SP_Database_MysqlAbstract
{
	private $host='localhost';
	private $user;
	private $pass;
	private $connect;
	private $logger;
	private $dbname;
	private $result;
	private $dbselected;
	private $last_id;
	private $last_query;
	private $use_sp_logger=true;
	private $log_all_query=true;
	private $escape_query=true;
	private $die_on_error=true;
	var $iterator;
	private $show_error=false;
	
	function __construct($dbname,$user,$pass,$host='localhost')
	{
		$this->host=$host;
		$this->dbname=$dbname;
		$this->user=$user;
		$this->pass=$pass;
		$this->dbselected=false;
		if( $this->use_sp_logger )
		{
			$this->logger = new SP_Common_Logger;
		}
	}
	
	function __destruct()
	{
		if( $this->connect ) mysql_close($this->connect);
	}
	
	public function getConnection()
	{
		return $this->connect;
	}
	
	public function escapeQuery($flag)
	{
		$escape_query=$flag;
	}
	
	public function getBaseName()
	{
		return $this->dbname;
	}
	
	public function setEncoding($enc)
	{
		$this->Query('SET NAMES '.$enc);
	}
	
	public function getLastQuery()
	{
		return $this->last_query;
	}
	
	public function getResult()
	{
		return $this->result;
	}
	
	public function getLastInsertId()
	{
		return $this->last_id;
	}
	
	public function realEscape($str)
	{
		return mysql_real_escape_string($str);
	}
	
	public function reConnect($dbname,$user,$pass)
	{
		$this->closeConnect();
		$this->dbname=$dbname;
		$this->user=$user;
		$this->pass=$pass;
		$this->dbselected=false;
		$this->Connect();		
	}
	
	public function changeUser($user,$pass)
	{
		$this->reConnect($this->dbname,$user,$pass);
	}
	
	public function changeBase($dbname)
	{
		$this->reConnect($dbname,$this->user,$this->pass);
	}
	
	public function Connect()
	{
		if( !$this->connect )
		{
			$this->connect = @mysql_connect($this->host, $this->user, $this->pass);
			if( !$this->connect)
			{
				if( $this->logger ) $this->logger->databaseLog( $this->getError() );
				if( $this->die_on_error ) die( ($this->show_error)? $this->getError() : 'Can not connect to database server' );
			}
		}
		return $this->connect;
	}
	
	public function selectBase()
	{
		if ( !$this->Connect() ) return false;
		if( !$this->dbselected )
		{
			$this->dbselected=@mysql_select_db($this->dbname, $this->connect);
			if( !$this->dbselected )
			{
				if( $this->logger ) $this->logger->databaseLog( $this->getError() );
				if( $this->die_on_error ) die( ($this->show_error)? $this->getError() : 'Can not connect to database' );
			}
		}
		return $this->dbselected;
	}
	
	public function showError($value)
	{
		$this->show_error=(bool)$value;
	}
	
	public function dieOnError($value)
	{
		$this->die_on_error=(bool)$value;
	}
	
	public function useSpLogger($value)
	{
		$this->use_sp_logger=(bool)$value;
	}
	
	public function logAllQuery($value)
	{
		$this->log_all_query=(bool)$value;
	}
	
	public function setLogger($object)
	{
		$this->logger=$object;
	}
	
	public function Query($query)
	{
		if( $this->selectBase() )
		{
			$this->last_query=$query;
			if( $this->logger and $this->log_all_query )
			{
				$this->logger->databaseLog( $this->last_query );
			}
			if ($escape_query) $query=$this->realEscape($query);
			$this->result=@mysql_query($query, $this->connect);
			if( $this->result )
			{
				$this->last_id=mysql_insert_id();
				$this->iterator=0;
			}
			else
			{
				if( $this->logger ) $this->logger->databaseLog( $this->getError() );
				if( $this->die_on_error ) die( ($this->show_error)? $this->getError() : 'Query error' );
			}
			return $this->result;
		}
		return false;
	}
	
	public function fetchAssoc()
	{
		$this->iterator+=1;
		if ($this->result)
		{
			return mysql_fetch_assoc($this->result);
		}
		else
		{
			return NULL;
		}
	}
	
	public function getAssocArray($query)
	{
		$array=array();
		$this->Query($query);
		while($row=$this->fetchAssoc())
		{
			$array[]=$row;
		}
		return $array;
	}
	
	public function getNumRows()
	{
		if ($this->result)
		{
			return mysql_num_rows($this->result);
		}
		else
		{
			return NULL;
		}
	}
	
	public function getAffectedRows()
	{
		if ($this->getConnection())
		{
			return mysql_affected_rows($this->getConnection());
		}
		else
		{
			return NULL;
		}
	}
	
	public function getField($field,$query)
	{
		$query=sprintf($query,$field);
		$this->query($query);
		if(!$this->getResult()) return NULL;
		if($this->getNumRows()==1)
		{
			$row=$this->fetchAssoc();
			return $row[$field];
		}
		else
		{
			while($row=$this->fetchAssoc())
			{
				$mas[]=$row[$field];
			}
			return $mas;
		}
	}
	
	public function getAssocRow($query)
	{
		$this->Query($query);
		return $this->fetchAssoc();
	}
	
	public function getAssocField($query,$field,$id_field='id')
	{
		$array=array();
		$this->Query($query);
		while($row=$this->fetchAssoc())
		{
			$array[$row[$id_field]]=$row[$field];
		}
		return $array;
	}
	
	public function getAssocArrayId($query,$id_field='id')
	{
		$array=array();
		$this->Query($query);
		while($row=$this->fetchAssoc())
		{
			$array[$row[$id_field]]=$row;
		}
		return $array;
	}
	
	public function closeConnect()
	{
		if ($this->connect) mysql_close($this->connect);
	}
	
	static public function staticQuery($query)
	{
		return mysql_query($query);
	}
	
	static public function staticFetchAssoc($result)
	{
		return mysql_fetch_assoc($result);
	}
	
	public function getError()
	{
		return mysql_error();
	}
	
}
?>
